package com.woniu.aiclient.service.impl;
import com.alibaba.cloud.ai.graph.CompiledGraph;
import com.alibaba.fastjson2.JSONObject;
import com.google.gson.Gson;
import com.woniu.aiclient.service.RecommendedService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Map;

@Service
public class RecommendedServiceImpl implements RecommendedService {
    @Resource
    private CompiledGraph compiledGraph;

    @Override
    public Object getRecommendedPlaces(String input) {

        Object result = compiledGraph.invoke(Map.of("input", input))
                .flatMap(state -> state.value("classifier_output", String.class))
                .orElse("");
        compiledGraph.invoke(Map.of("input", input));
        if (result instanceof String strResult) {
            if (strResult.startsWith("```json")) {
                result = strResult.replace("```json", "")
                        .replace("```", "")
                        .trim();
            }
            try {
                result = new Gson().fromJson((String) result, Map.class);
            } catch (Exception e) {
                // 解析失败保持原样
            }
        }
        return result;
    }
}
